<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c job xs f">

  <xsl:import href="../style/base.xsl" />
  
  <xsl:output encoding="UTF-8"
    doctype-public="-//W3C//DTD HTML 4.01//EN"
    doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
  
  <xsl:param name="ref" select="'/localhost/system/work'"/>
  <xsl:variable name="responseId" select="'/livcos.org/web/admin/JobStatistics'"/>
  
  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Job Statistics</title>
        <link href="../css/global.css" rel="stylesheet" type="text/css" />
      	<script type="text/javascript" src="../script/util.js"><xsl:comment> // </xsl:comment></script>
      	<script type="text/javascript" src="../script/E.js"><xsl:comment> // </xsl:comment></script>
      </head>
      <body>
        <h2>
          <xsl:text>Job Statistics </xsl:text>
          <a href="JobStatistics.view">
            <img src="../img/refresh.gif" border="0" alt="refresh"/>
          </a>
          <xsl:text> </xsl:text>
          <a href="clear?objId=/localhost/system/work&amp;responseId={$responseId}">
            <img src="../img/clear.gif" border="0" alt="clear"/>
          </a>
        </h2>
        <xsl:apply-templates select="doc(concat($ref, '.cosmos'))/*"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="*">
    <xsl:variable name="lastState" select="state[last()]"/>
    <tr>
      <td>
        <a href="../meta?ref={$ref}/{name(.)}">
          <xsl:value-of
            select="format-dateTime(xs:dateTime(state[1]/@time), '[Y0001]-[M01]-[D01]&#160;[H01]:[m01]:[s01]')"/>
        </a>
      </td>
      <td>
        <xsl:call-template name="print_duration">
          <xsl:with-param name="duration" select="xs:dateTime($lastState/@time) - xs:dateTime(state[1]/@time)"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:call-template name="print_memory">
          <xsl:with-param name="memory" select="$lastState/@totalMemory"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:call-template name="print_memory">
          <xsl:with-param name="memory" select="$lastState/@freeMemory"/>
        </xsl:call-template>
      </td>
      <td align="right">
        <xsl:value-of select="count(job:*)"/>
      </td>
      <td>
        <a href="{c:request/url/@path}?_job={name(.)}">
          <xsl:value-of select="c:request/url/@path"/>
        </a>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="/c:work">
    <table border="1" cellspacing="0" cellpadding="1" style="font-size: 80%;">
      <thead>
        <tr>
          <th>Time</th>
          <th>Duration</th>
          <th>Memory Total</th>
          <th>Memory Free</th>
          <th>Tasks</th>
          <th>Data</th>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="*">
          <xsl:sort select="name(.)" order="descending"/>
        </xsl:apply-templates>
      </tbody>
    </table>
  </xsl:template>
  
  <xsl:template name="print_duration">
    <xsl:param name="duration"/>
    <div style="float: right; font-size: 80%;">
      <xsl:value-of select="f:format-duration($duration)"/>
    </div>
    <xsl:variable name="seconds" select="f:total-seconds($duration)"/>
    <xsl:choose>
      <xsl:when test="$seconds > 5">
        <div style="background-color: red; width: {$seconds}px;">&#160;</div>
      </xsl:when>
      <xsl:otherwise>
        <div style="background-color: #00CC00; width: {$seconds * 20}px;">&#160;</div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="print_memory">
    <xsl:param name="memory"/>
    <xsl:variable name="mem" select="$memory div 1024 div 1024"/>
    <div style="float: right; font-size: 80%;">
      <xsl:value-of select="format-number($mem, '0.000')"/>
    </div>
    <xsl:choose>
      <xsl:when test="$mem > 4000">
        <div style="background-color: #CCCC00; width: {$mem div 80}px;">&#160;</div>
      </xsl:when>
      <xsl:when test="$mem > 1000">
        <div style="background-color: #CC00CC; width: {$mem div 20}px;">&#160;</div>
      </xsl:when>
      <xsl:when test="$mem > 200">
        <div style="background-color: #00CCCC; width: {$mem div 5}px;">&#160;</div>
      </xsl:when>
      <xsl:otherwise>
        <div style="background-color: #00CC00; width: {$mem}px;">&#160;</div>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>
